home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip
/
MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf
/
Programming
/
Random
/
chisqr.c
< prev
next >
Wrap
Text File
|
1992-03-30
|
1KB
|
33 lines
/* This is the chi-squared test for whether a sequence is as well
distributed as a random sequence. The return result should be within
2*sqrt(R) of R, provided N is at least 10*R. However, test your generator
with a few different seeds; the test is wrong about 1 time in 10.
* N is the number of random numbers to generate.
* R is the range of random numbers; integers in the range 0 to R-1 will
be generated. Note that this parameter also controls how much memory will
be used (4*R bytes).
* seed is the initial seed for the generator.
Code & comments generated 14 Feb 92 by Peter Thompson almost verbatim
from "Algorithms in C", Robert Sedgewick, pub. 1990 by Addison-Wesley. It a
good book, but not as detailed as where HE got the algorithm from : "The
Art Of Computer Programming", D.E. Knuth, I think volume 2.
*/
float chisqr(long N, long R, long seed)
{ /* chisqr test */
long i;
long *counts;
double sum = 0;
counts = (long *) calloc(R,sizeof(long int));
if counts {
randinit(seed);
for (i = N; (i > 0); i--)
counts[randomint(R)]++;
for (i = R-1; (i >= 0); i--)
sum += counts[i]*counts[i];
free(counts);
}
return (float)(R*(sum/(double)N)-N);
} /* chisqr test */